<?php

namespace app\admin\controller\goods;

use app\common\controller\AdminController;
use app\common\model\GoodsLibrary;
use app\common\model\GoodsLibraryShopRelation;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="goods_library_shop_relation")
 */
class LibraryShopRelation extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\GoodsLibraryShopRelation();
        
    }


    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $libraryId = input('library_id',0,'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where,$extra) = $this->buildTableParames(['shop_name']);
            if($libraryId){
                array_unshift($where,['ggr.goods_library_id','=',$libraryId]);
            }
            if(isset($extra['shop_name'])){
                $where[] = ['g.name','like','%'.$extra['shop_name'].'%'];
            }

            $count = $this->model->alias('ggr')
                ->leftJoin('goods_library gl','gl.id=ggr.goods_library_id')
                ->leftJoin('shop g','g.id=ggr.shop_id')
                ->where($where)
                ->count();
            $list = $this->model->alias('ggr')
                ->leftJoin('goods_library gl','gl.id=ggr.goods_library_id')
                ->leftJoin('shop g','g.id=ggr.shop_id')
                ->where($where)
                ->field('ggr.*,gl.name as library_name,g.name as shop_name')
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('library_id',$libraryId);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $libraryId = input('library_id',0,'intval');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'shop_id|店铺' => 'require'
            ];
            $this->validate($post, $rule);
            try {
                $insertData = [];
                $shopId = explode(',',input('shop_id'));
                foreach($shopId as $gid){
                    $insertData[] = [
                        'goods_library_id' => $libraryId,
                        'shop_id' => $gid,
                        'status' => input('status',0,'intval')
                    ];
                }
                $save = (new GoodsLibraryShopRelation())->saveAll($insertData);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $this->success('保存成功');
        }
        $library = GoodsLibrary::where('id',$libraryId)->field('id,name')->find();
        $this->assign('library',$library);
        $existGoodsId = implode(',',GoodsLibraryShopRelation::where('goods_library_id',$libraryId)->column('shop_id'));
        $this->assign('exist_goods_id',$existGoodsId);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'shop_id|店铺' => 'require'
            ];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        $library = GoodsLibrary::where('id',$row['goods_library_id'])->field('id,name')->find();
        $this->assign('library',$library);
        return $this->fetch();
    }
    
}